package com.cx.concurrent;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Author:CHENXIAOYI
 * Since:2020/9/22 16:09
 */
public class ExecutorsTest {

    private static final ExecutorService e1 =  Executors.newSingleThreadExecutor();
    private static final ExecutorService e2 =  Executors.newFixedThreadPool(10);
    private static final ExecutorService e3 =  Executors.newCachedThreadPool();


    public static void main(String[] args) throws InterruptedException {
        long l = System.currentTimeMillis();
        List<Integer> list = new ArrayList<>();
        Random random = new Random();

        for (int i = 0; i < 100000; i++) {
//            Thread thread = new Thread(() -> {
//                list.add(random.nextInt());
//            }, String.valueOf(i));
//            thread.start();
//            thread.join();
            e2.execute(()->{  list.add(random.nextInt());});
        }
        e1.shutdown();
        e1.awaitTermination(1, TimeUnit.DAYS);
        System.out.println(System.currentTimeMillis() - l);
        System.out.println(list.size());

    }





    private static void testRunStart() {
        MyThread thread = new MyThread();
        thread.run(); //调用run方法让线程执行  //依旧在main线程中
        thread.start(); //调用start0()  创建jvm---javaThread新的线程 并执行 属于多线程执行
        System.out.println("ExecutorsTest.main");
    }


    static class MyThread extends Thread{
        @Override
        public void run() {
            System.out.println("****我run***");
        }
    }

}
